package com.msconfig.mvp.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.event.ProjectionDialogDismissEvent;
import com.msconfig.mvp.view.adapter.ProjectionAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import static com.msconfig.mvp.view.fragment.SetupProjectionParameterFragment.RLPROJECTIONMODE;

public class SetupProjectionSelectDialogActivity extends Activity {
    private ArrayList<String> arrayList;
    private ListView listView;
    private TextView tvTitle;
    private ProjectionAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_projection_select_dialog);
        tvTitle=findViewById(R.id.title);
        listView=findViewById(R.id.lv_listview);
        arrayList=new ArrayList<>();
        arrayList.add(getString(R.string.gauss_projection));
        arrayList.add(getString(R.string.utm_projection));
        arrayList.add(getString(R.string.transverse_mercator_projection));
        arrayList.add(getString(R.string.oblique_stereographic_projection));
        arrayList.add(getString(R.string.double_stereographic_projection));
        adapter=new ProjectionAdapter(this,arrayList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setCurrentItem(position);
                adapter.setmIsClick(true);
                //刷新这个才能重新执行getView方法
                adapter.notifyDataSetChanged();
                TextView tvName=view.findViewById(R.id.tv_name);
                String strName=tvName.getText().toString().trim();
                //更新参数设置页面
                EventBus.getDefault().post(new ProjectionDialogDismissEvent(strName));
                finish();
            }
        });

        Intent intent=getIntent();
        String data = intent.getStringExtra(RLPROJECTIONMODE);
        int index=arrayList.indexOf(data);
        if(index==-1){
            adapter.setCurrentItem(0);
            adapter.setmIsClick(true);
        }else{
            adapter.setCurrentItem(index);
            adapter.setmIsClick(true);
        }
    }
}
